package com.example.fms.controller;

import com.example.fms.dto.resp.MenusDto;
import com.example.fms.dto.resp.RoleRightDTO;
import com.example.fms.service.RightService;
import com.example.fms.service.RoleService;
import com.example.fms.vo.ResponseBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {
    @Resource
    private RoleService roleService;


    //获取角色权限
    @GetMapping()
    public ResponseBean<List<RoleRightDTO>> getMenus() {
        return ResponseBean.successMeat(roleService.getRoles());
    }

    //新增角色
    @PostMapping()
    public ResponseBean<Boolean> rolesPost(@RequestBody RoleRightDTO req) {
        return ResponseBean.successMeat(roleService.rolesPost(req));
    }

    //根据id查询角色
    @GetMapping("/{id}")
    public ResponseBean<RoleRightDTO> getRoleById(@PathVariable("id") Long id) {
            return ResponseBean.successMeat(roleService.getRolesById(id));
    }


    //编辑提交角色
    @PutMapping("/{id}")
    public ResponseBean<Boolean> updateRoleById(@RequestBody RoleRightDTO req,@PathVariable("id") Long id) {
        return ResponseBean.successMeat(roleService.updateRoleById(req,id));
    }

    //删除角色
    @DeleteMapping("/{id}")
    public ResponseBean<Boolean> deleteRoleById(@PathVariable("id") Long id) {
        return ResponseBean.successMeat(roleService.deleteRoleById(id));
    }

    //角色授权
    @PostMapping("/{id}/rights")
    public ResponseBean<Boolean> saveRights(@PathVariable("id") Long id,@RequestBody RoleRightDTO req) {
        return ResponseBean.successMeat(roleService.saveRights(id,req));
    }

    //删除角色指定的权限
    @DeleteMapping("/{id}/rights/{rightId}")
    public ResponseBean<List<MenusDto>> deleteRoleRightsById(@PathVariable("id") Long id, @PathVariable Long rightId) {
        return ResponseBean.successMeat(roleService.deleteRoleRightsById(id,rightId));
    }
}
